<template>
	<view class="">
		<HeadCustom></HeadCustom>

		<view class="page-select-major">
			<u-popup :round="10" :show="statusShow" mode="center" @close="closeStatusShow">
				<view class="status-popup-content">
					<view class="item" @click="goStudentList(1)">
						<view class="">
							调剂名单
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
					<view class="item" @click="goStudentList(2)">
						<view class="">
							录取名单
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</u-popup>
			<view class="">
				选择专业
			</view>
			<view class="select-box">
				<view class="select-item" v-for="(item,index) in formData" :key="index" @click="openSheet(index)">
					<view class="label">
						{{item.label}}：
					</view>
					<view class="placeholder">
						{{item.value ? item.value : item.placeholder}}
						<u-icon :name="openSheetIndex == index ? 'arrow-up':'arrow-down'"></u-icon>
					</view>
				</view>
			</view>
			<u-button type="primary" text="查看详情" @click="goStudenList"></u-button>
			<view>
				<u-popup :show="show" @close="close(0)" :closeOnClickOverlay="true">
					<view class="popup-content">
						<view class="">
							{{degree.title}}
						</view>
						<view class="item-box">
							<view class="item" v-for="(item,index) in degree.list" :key="index" @click="select(0,index)">
								<view :class="item.selected ? 'selected' : ''">
									{{item.name}}
								</view>
								<u-icon v-if="item.selected" name="checkmark-circle" color="#27bc83"></u-icon>
							</view>
						</view>
					</view>
				</u-popup>
			</view>
			<view class="majorShow">
				<u-popup :show="majorShow" @close="close(1)" :closeOnClickOverlay="true">
					<view class="popup-content">
						<view class="">
							选择专业
						</view>
						<view class="search">
							<u-search placeholder="请输入专业名称" @custom="search" v-model="searchKey" @change="search"></u-search>
						</view>
						<view class="item-box">
							<view class="item" v-for="(item,index) in searchMajor.list" :key="index" @click="select(1,index)">
								<view :class="item.selected ? 'selected' : ''">
									{{item.name}}
								</view>
								<u-icon v-if="item.selected" name="checkmark-circle" color="#27bc83"></u-icon>
							</view>
						</view>
					</view>
				</u-popup>
			</view>
			<view class="collegeShow">
				<u-popup :show="collegeShow" @close="close(2)" :closeOnClickOverlay="true">
					<view class="popup-content">
						<view class="">
							{{degree.title}}
						</view>
						<view class="item-box">
							<view class="item" v-for="(item,index) in college.list" :key="index" @click="select(2,index)">
								<view :class="item.selected ? 'selected' : ''">
									{{item.name}}
								</view>
								<u-icon v-if="item.selected" name="checkmark-circle" color="#27bc83"></u-icon>
							</view>
						</view>
					</view>
				</u-popup>
			</view>
			<view class="studyShow">
				<u-popup :show="studyShow" @close="close(3)" :closeOnClickOverlay="true">
					<view class="popup-content">
						<view class="">
							{{degree.title}}
						</view>
						<view class="item-box">
							<view class="item" v-for="(item,index) in study.list" :key="index" @click="select(3,index)">
								<view :class="item.selected ? 'selected' : ''">
									{{item.name}}
								</view>
								<u-icon v-if="item.selected" name="checkmark-circle" color="#27bc83"></u-icon>
							</view>
						</view>
					</view>
				</u-popup>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getmajorList
	} from '@/utils/api.js'
	export default {
		onLoad(option) {
			this.marjorParams.schoolName = option.schoolName
			this.getmajorList()
		},
		data() {
			return {
				statusShow: false,
				searchKey:'',
				degree: {
					title: '选择学位类型',
					list: [{
							name: '学术型',
							code: '1',
							selected: false,
						},
						{
							name: '专业型',
							code: '2',
							selected: false,
						}
					]
				},
				show: false,
				majorShow: false,
				collegeShow: false,
				studyShow: false,
				majorOutRelationId: '',
				openSheetIndex: -1,
				marjorParams: {
					schoolName: '',
					subjectCategory: '',
					collegeName: '',
					applyLearningStyle: '',
					majorName: '',
					educateType: '',
					pageNum: '1',
					pageSize: '999'
				},
				majorList: [],
				selectMajor: {
					title: '选择专业',
					list: [],
				},
				searchMajor:{
					title: '选择专业',
					list: [],
				},
				college: {
					title: '选择专业',
					list: [],
				},
				study: {
					title: '选择学习方式',
					list: [],
				},
				formData: [{
						label: '学位类型',
						value: 0,
						placeholder: '请选择学位类型',
					},
					{
						label: '报考专业',
						value: 0,
						placeholder: '请选择报考专业',
						code: 0,
					},
					{
						label: '报考学院',
						value: 0,
						placeholder: '请选择报考学院',
					},
					{
						label: '学习方式',
						value: 0,
						placeholder: '请选择学习方式',
					}
				]
			};
		},

		watch: {
			formData: {
				handler(newvalue, oldvalue) {

				},
				deep: true
			}
		},
		methods: {
			goStudentList(status) {
				uni.navigateTo({
					url: '/schoolMsg/studentList/index?majorOutRelationId=' + this.majorOutRelationId + '&status=' + status +
						'&schoolName=' + this.marjorParams.schoolName
				})
			},
			closeStatusShow() {
				this.statusShow = false
			},
			uniqueFunc(arr) {
			    const res = []
			    arr.forEach(item => {
			        if(typeof item === 'object' && item !== null){
			            const tmp = res.filter(i => JSON.stringify(i) === JSON.stringify(item));
			            if(tmp.length === 0) {
			                res.push(item)
			            }
			        }
			    })
			    return res;
			},
			search() {
				if(this.searchKey == "") {
					this.searchMajor.list = this.selectMajor.list
					return
				}
				this.searchMajor.list = this.selectMajor.list.filter((item,index) => {
					return item.name.includes(this.searchKey)
				})
				console.log('this.searchMajor.list',this.searchMajor.list)
			},
			// type:0 学位 1专业 2学院 3学习方式
			select(type, index) {
				if (type == 0) {
					this.degree.list.forEach((item) => {
						item.selected = false
					})
					this.degree.list[index].selected = true
					this.show = false
					this.formData[type].value = this.degree.list[index].name
					this.selectMajor.list = []
					this.majorList.forEach((item) => {
						if (item.educateType == this.formData[0].value) {
							this.selectMajor.list.push({
								name: `(${item.majorCode})${item.majorName}`,
								code: item.majorCode,
								selected: false
							})
						}
					})
					
					
					// console.log('uniqueArr',uniqueArr)
				} else if (type == 1) {
					this.searchMajor.list.forEach((item) => {
						item.selected = false
					})
					this.searchMajor.list[index].selected = true
					this.majorShow = false
					this.formData[2].value = ''
					this.formData[3].value = ''
					this.formData[type].value = this.searchMajor.list[index].name
					this.formData[type].code = this.searchMajor.list[index].code
					this.college.list = []
					this.majorList.forEach((item) => {
						if (item.educateType == this.formData[0].value) {
							if (item.majorCode == this.formData[1].code) {
								this.college.list.push({
									name: item.collegeName,
									selected: false,

								})

							}
						}
					})
				} else if (type == 2) {
					this.college.list.forEach((item) => {
						item.selected = false
					})
					this.college.list[index].selected = true

					this.collegeShow = false
					this.formData[3].value = ''
					this.formData[type].value = this.college.list[index].name
					this.study.list = []
					this.majorList.forEach((item) => {
						if (item.educateType == this.formData[0].value) {
							if (item.majorCode == this.formData[1].code) {
								if (item.collegeName == this.formData[2].value) {
									this.study.list.push({
										name: item.applyLearningStyle,
										selected: false,
										majorOutRelationId: item.majorOutRelationId
									})
								}
							}
						}
					})
				} else if (type == 3) {
					this.study.list.forEach((item) => {
						item.selected = false
					})
					this.study.list[index].selected = true
					this.studyShow = false
					this.formData[type].value = this.study.list[index].name
					// 找到唯一的majorOutRelationId
					this.majorOutRelationId = this.study.list[index].majorOutRelationId
					// console.log('majorOutRelationId',majorOutRelationId)
				}
				this.selectMajor.list = this.uniqueFunc(this.selectMajor.list)
				this.searchMajor.list = this.selectMajor.list
				this.openSheetIndex = -1
			},
			openSheet(index) {
				this.openSheetIndex = index
				if (index == 0) {
					this.show = true
				} else if (index == 1) {
					if (this.formData[0].value == 0) {
						uni.showToast({
							title: '请依次选择',
							icon: 'none'
						})
						return
					}
					this.search()
					this.majorShow = true
				} else if (index == 2) {
					if (this.formData[1].value == 0) {
						uni.showToast({
							title: '请依次选择',
							icon: 'none'
						})
						return
					}
					this.collegeShow = true
				} else if (index == 3) {
					if (this.formData[2].value == 0) {
						uni.showToast({
							title: '请依次选择',
							icon: 'none'
						})
						return
					}
					this.studyShow = true
				}

			},
			async getmajorList() {
				uni.showLoading({
					title: '加载中'
				});
				// console.log('this.marjorParams', this.marjorParams)
				let res = await getmajorList(this.marjorParams)
				if (res.code == 200) {
					this.majorList = res.rows
				}
				let teplist = this.majorList.filter((item, index) => {
					return (item.educateType != '学术型' && item.educateType != '专业型')
				})
				uni.hideLoading();

			},
			goStudenList() {
				if (this.formData[3].value == 0) {
					uni.showToast({
						title: '请依次选择',
						icon: 'none'
					})
					return
				}
				this.statusShow = true

			},
			close(index) {
				if (index == 0) {
					this.show = false
				} else if (index == 1) {
					this.majorShow = false
				} else if (index == 2) {
					this.collegeShow = false
				} else if (index == 3) {
					this.studyShow = false
				}

				this.openSheetIndex = -1

			}
		}
	};
</script>


<style scoped lang="scss">
	.page-select-major {
		padding: 20rpx;
		::v-deep.u-safe-bottom {
			padding-bottom:0 !important;
		}
		.status-popup-content {
			padding: 20rpx;

			.item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 600rpx;
				height: 70rpx;
				line-height: 70rpx;
			}
		}

		.select-item {
			display: flex;
			height: 100rpx;
			line-height: 100rpx;
			margin-bottom: 20rpx;

			.placeholder {
				display: flex;
				flex: 1;
				justify-content: space-between;
				border-radius: 10rpx;
				padding: 0 10rpx;
				background-color: #f8f8f8;
			}
		}

		.popup-content {
			padding: 20rpx;

			.item-box {
				height: 600rpx;
				overflow: auto;
			}

			.item {
				display: flex;
				margin-top: 10rpx;
				justify-content: space-between;

				.selected {
					color: #27bc6a;
				}
			}
		}

	}
</style>